package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.material.tabs.TabLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.HotAdapter;
import flc.ast.databinding.ActivityExpressBinding;
import flc.ast.fragment.TabFragment;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.view.container.StkLinearLayout;
import stark.common.bean.StkResBean;

public class ExpressActivity extends BaseAc<ActivityExpressBinding> {

    private List<Fragment> fragments = new ArrayList<>();
    private String[] tabTitle = new String[]{};
    private String[] tabUrl = new String[]{};

    @Override
    protected int onCreate() {
        return R.layout.activity_express;
    }

    @Override
    protected void initView() {
        mDataBinding.ivExpressBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        tabTitle = new String[]{getString(R.string.caricature_express), getString(R.string.caricature_video)};
        tabUrl = new String[]{AppConfig.EXPRESS_DATA, AppConfig.VIDEO_DATA};
    }

    @Override
    protected void initData() {
        for (int i = 0; i < tabTitle.length; i++) {
            fragments.add(new TabFragment().newInstance(tabUrl[i]));
            mDataBinding.tlExpress.addTab(mDataBinding.tlExpress.newTab().setText(tabTitle[i]));
        }
        mDataBinding.vpExpress.setAdapter(new ArticleAdapter(getSupportFragmentManager()));
        mDataBinding.tlExpress.setupWithViewPager(mDataBinding.vpExpress);
        //给tab每个item设置值
        for (int i = 0; i < mDataBinding.tlExpress.getTabCount(); i++) {
            TabLayout.Tab tab = mDataBinding.tlExpress.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(getTabView(i));
            }
        }
        // 设置默认第一个被选中
        View view = mDataBinding.tlExpress.getTabAt(0).getCustomView();
        TextView tv = view.findViewById(R.id.tvTabItemName);
        tv.setTextColor(Color.parseColor("#FFFFFFFF"));
        StkLinearLayout llTabItem=view.findViewById(R.id.llTabItem);
        llTabItem.setBackgroundResource(R.drawable.axuanzhong);
        StkLinearLayout llTabItemBg=view.findViewById(R.id.llTabItemBg);
        llTabItemBg.setPadding(10,10,0,10);
        //设置TabLayout点击事件
        mDataBinding.tlExpress.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                TextView tv = view.findViewById(R.id.tvTabItemName);
                tv.setTextColor(Color.parseColor("#FFFFFFFF"));
                StkLinearLayout llTabItem=view.findViewById(R.id.llTabItem);
                llTabItem.setBackgroundResource(R.drawable.axuanzhong);
                StkLinearLayout llTabItemBg=view.findViewById(R.id.llTabItemBg);
                if (tab.getPosition()==0){
                    llTabItemBg.setPadding(10,10,0,10);
                }else {
                    llTabItemBg.setPadding(0,10,10,10);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                TextView tv = view.findViewById(R.id.tvTabItemName);
                tv.setTextColor(Color.parseColor("#FF000000"));
                StkLinearLayout llTabItem=view.findViewById(R.id.llTabItem);
                llTabItem.setBackgroundResource(0);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private class ArticleAdapter extends FragmentStatePagerAdapter {
        public ArticleAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        //此方法用来显示tab上的名字
        @Override
        public CharSequence getPageTitle(int position) {
            return tabTitle[position];
        }
    }

    //自定义Tab布局
    private View getTabView(int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_tab, null);
        TextView text = view.findViewById(R.id.tvTabItemName);
        text.setText(tabTitle[i]);
        text.setTextColor(Color.parseColor("#FF000000"));
        StkLinearLayout llTabItem=view.findViewById(R.id.llTabItem);
        llTabItem.setBackgroundResource(0);
        return view;
    }

}